LInteger()
This constructor creates a LInteger
with "NULL" magnitude.
(This is not equivalent to zero magnitude in this library.) Be
careful to assign a magnitude to the returned LInteger
before using it other than as an lvalue.
LInteger::LInteger(const unsigned int* magnitude,
const int numDigits, const int sign=0, const int copyMag=1)
This constructor creates a LInteger
representing the number determined by the first three arguments.
(See the Internal Representation of
LIntegers
section for details on the
required format.)
If copyMag
is non-zero the new instance will make its own
copy of the magnitude, so that the caller may retain ownership of the
memory pointed to by magnitude
. If copyMag
is zero, the library will assume control of the memory, and will attempt
to delete the memory pointed to by mangnitude
upon
destruction of the new instance.
Warning:
Be sure that the magnitude of any new LInteger
you create with this
constructor does not have lead zeroes. This is necessary for the
internal consistency of the library. If you don't feel like
checking if the magnitude you use has lead zeroes or not, call
compress()
immediately after creation of the new instance.
LInteger
s
LInteger::LInteger(const LInteger& a)
This copy constructor creates a LInteger
which represents
the same integer that a
does.
unsigned int
s
LInteger::LInteger(const unsigned int mag, const int sign=0)
Creates a LInteger
of single digit magnitude whose
sign is determined by the sign
argument.
(See the Internal Representation of
LIntegers
section for details on the
required value.)
int
s
LInteger::LInteger(const int mag)
Creates a LInteger
representing the same integer
that mag
represents.
It is imporant for the internal consistency of the library that
operations always be done on LInteger
s with no
lead zeros in their magnitude. The two methods below are included to
help programmers insure that a LInteger
has no lead zeros.
Normally, they will not need to be called, however, since
all overloaded operators ensure that the instances they return
or modify have no lead zeros.
compressable
inline int LInteger::compressable() const;
Returns a non-zero int
iff the calling instance
has lead zeros in its magnitude.
compress
void LInteger::compress()
Strips the lead zeros, if any, from the calling instance's magnitude and adjusts the size accordingly.
inline int LInteger::sign() const
Returns an integer representing the calling instance's sign.
(See the Internal Representation of
LIntegers
section for details on the
required value.)
inline void LInteger::setSign(const int newSign)
Sets the sign of the calling instance equal to the value given by
newSign
.
inline int LInteger::size() const
Returns an integer representing the calling instance's size.
inline void LInteger::setSize(const int newSize)
Sets the integer representing the number of digits in the calling
instance's magnitude equal to the value given by newSize
.
magnitude
inline unsigned int* LInteger::magnitude() const
Returns a pointer to the calling instance's magnitude.
LInteger
: a=b
LInteger& LInteger::operator=(const LInteger& a)
Makes the calling instance represent an integer equal to
the integer represented by a
and returns a
reference to the modified calling instance.
-a
friend inline LInteger operator-(const LInteger& a)
Returns a new LInteger
representing the proudct of
-1 and the integer represented by a
.
+a
friend inline LInteger operator+(const LInteger& a)
Returns a new LInteger
representing the
same integer that a
represents. *snore*
a+b
friend LInteger operator+(const LInteger& a , const LInteger& b)
Returns a new LInteger
representing the sum of the
integers represented by a
and b
.
a+=b
inline LInteger& LInteger::operator+=(const LInteger& b)
Makes the calling instance represent the sum of the integer it represents
before the call and the integer b
represents.
Returns a reference to the modified calling instance.
a++
inline LInteger& LInteger::operator++(int) // postfix
Makes the calling instance represent the integer one greater than the integer it represented before the call. Returns a reference to the modified calling instance.
++a
inline LInteger& LInteger::operator++() // postfix
Makes the calling instance represent the integer one greater than the integer it represented before the call. Returns a reference to the modified calling instance. (Where have I heard this before? :) )
a-b
friend LInteger operator-(const LInteger& a , const LInteger& b)
Returns a new LInteger
representing the
integer represented by a
minus
the integer represneted by b
.
a-=b
inline LInteger& LInteger::operator-=(const LInteger& b)
Makes the calling instance represent the the integer it represents
before the call minus the integer b
represents.
Returns a reference to the modified calling instance.
a--
inline LInteger& LInteger::operator--(int)
Makes the calling instance represent the integer one less than the integer it represented before the call. Returns a reference to the modified calling instance.
--a
inline LInteger& LInteger::operator--()
Makes the calling instance represent the integer one less than the integer it represented before the call. Returns a reference to the modified calling instance. (Sound famaliar? :) )
a*b
friend LInteger operator*(const LInteger& a , const LInteger& b)
Returns a new LInteger
representing the product of the
integer represented by a
and the integer represneted
by b
.
a*=b
inline LInteger& LInteger::operator*=(const LInteger& b)
Makes the calling instance represent the product of the integer it represents
before and the integer that b
represents.
Returns a reference to the modified calling instance.
a/b
friend LInteger operator/(const LInteger& a , const LInteger& b)
From number theory, it is know that given two integers a
and b
such that b
is not zero, there exists
q
and r
such that a=q*b+r
with
0<=r<|b|
. a/b
returns a
LInteger
representing the integer q
given by this theorem.
a/=b
inline LInteger& LInteger::operator/=(const LInteger& b)
Makes the calling instance represent the integer the return
value of a/b
would represent
(see directly above),
and returns a reference to the modified calling instance.
a%b
friend LInteger operator%(const LInteger& a , const LInteger& b)
From number theory, it is known that given two integers a
and b
such that b
is not zero, there exists
q
and r
such that a=q*b+r
and
0<=r<|b|
. a%b
returns a LInteger
representing the integer r
given by this theorem.
a%=b
inline LInteger& LInteger::operator%=(const LInteger& b)
Makes the calling instance represent the integer the return
value of a%b
would represent
(see directly above),
and returns a reference to the modified calling instance.
a^b
friend LInteger operator^(const LInteger& a, const LInteger& b)
Returns a new LIntegers
representing the
integer whose i
th most
significant bit is equal to exclusive OR of the i
th
most significant bit of the integer represented by a
and the i
th most significant bit of the integer
represented by b
.
Note: under current implementation, a
and
b
must represent non-negative integers with
the same number of digits in their magnitude.
a^=b
inline LInteger& LInteger::operator^=(const LInteger& b)
Makes the calling instance represent the integer the return
value of a^b
would represent
(see directly above).
Returns a reference to the modified calling instance.
Note: under current implementation, the calling instance
and b
must both represent non-negative integers with
the same number of digits in their magnitude.
a|b
friend LInteger operator|(const LInteger& a, const LInteger& b)
Returns a new LIntegers
representing the
integer whose i
th most
significant bit is equal to inclusive OR of the i
th
most significant bit of the integer represented by a
and the i
th most significant bit of the integer
represented by b
.
Note: under current implementation, a
and
b
must represent non-negative integers with
the same number of digits in their magnitude.
a|=b
inline LInteger& LInteger::operator|=(const LInteger& b)
Makes the calling instance represent the integer the return
value of a|b
would represent
(see directly above).
Returns a reference to the modified calling instance.
Note: under current implementation, the calling instance
and b
must both represent non-negative integers with
the same number of digits in their magnitude.
a&b
friend LInteger operator&(const LInteger& a, const LInteger& b)
Returns a new LIntegers
representing the
integer whose i
th most
significant bit is equal to the logical conjuction of the i
th
most significant bit of the integer represented by a
and the i
th most significant bit of the integer
represented by b
.
Note: under current implementation, a
and
b
must represent non-negative integers with
the same number of digits in their magnitude.
a&=b
inline LInteger& LInteger::operator&=(const LInteger& b)
Makes the calling instance represent the integer the return
value of a&b
would represent
(see directly above).
Returns a reference to the modified calling instance.
Note: under current implementation, the calling instance
and b
must both represent non-negative integers with
the same number of digits in their magnitude.
~a
friend LInteger operator~(const LInteger& a)
Returns a new LInteger
representing the
integer whose i
th most significant bit is
the logical negation of the i
th
most singficant bit of the integer represent by a
.
a>>distance
friend LInteger operator>>(const LInteger& a, const int distance)
Returns a new LInteger
representing the integer
whose i
th most significant bit is equal to the
i+distance
th most significant bit of the integer
represented by a
.
distance
can be much greater than
the number of bits in an unsigned int
.
Note: Under this implementation, distance
must
be non-negative.
a>>=distance
inline LInteger& LInteger::operator>>=(const int distance)
Makes the calling instance represent the integer the return
value of a>>distance
would represent
(see directly above).
Returns a reference to the modified calling instance.
Note: the same notes as for >>
apply.
(See directly above.).
a<<distance
friend LInteger operator<<(const LInteger& a, const int distance)
Returns a new LInteger
representing the integer
whose i
th least significant bit is equal to the
i-distance
th least significant bit of the integer
represented by a
. If i-distance
is
not positive, the i
th bit of the integer represented
by returned LInteger
will be zero.
distance
can be much greater than
the number of bits in an unsigned int
.
Note: Under this implementation, distance
must
be non-negative.
a<<=distance
inline LInteger& LInteger::operator<<=(const int distance)
Makes the calling instance represent the integer the return
value of a<<distance
would represent
(see directly above).
Returns a reference to the modified calling instance.
Note: the same notes as for <<
apply.
(See directly above.).
a<b
friend inline int operator<(const LInteger& a, const LInteger& b)
Returns a non-zero int
iff a
is less than
b
.
a>b
friend inline int operator>(const LInteger& a, const LInteger& b)
Returns a non-zero int
iff a
is greater than
b
.
a==b
friend inline int operator==(const LInteger& a, const LInteger& b)
Returns a non-zero int
iff a
is equal to
b
.
a!=b
friend inline int operator!=(const LInteger& a, const LInteger& b)
Returns a non-zero int
iff a
is not equal to
b
.
a<=b
friend inline int operator<=(const LInteger& a, const LInteger& b)
Returns a non-zero int
iff a
is less than
or equal to b
.
a>=b
friend inline int operator>=(const LInteger& a, const LInteger& b)
Returns a non-zero int
iff a
is greater than
or equal to b
.
!a
friend inline int operator!(const LInteger& a)
Returns a non-zero int
iff a
is non-zero.
a&&b
friend inline int operator&&(const LInteger& a,const int b)
friend inline int operator&&(const int a,const LInteger& b)
Returns a non-zero int
iff both of the arguments
are non-zero.
a||b
friend inline int operator||(const LInteger& a,const int b)
friend inline int operator||(const int a,const LInteger& b)
Returns a non-zero int
iff at least one of the arguments
is non-zero.
int
Conversion: int(a)
inline LInteger::operator int()
Returns an int
with the same sign as the calling instance,
and with magnitude equal to the (bits per unsigned int)-1
least significant bits of the calling instance's magnitude.
s<<a
friend ostream& operator<<(ostream& s, const LInteger& a)
Pushes a
onto the stream as follows: First, a '-' is
pushed onto the stream if a
is negative, otherwise a
'+' is pushed onto the stream. The magnitude of a
in hexadecimal is then pushed onto the stream.